package com.example.controller.api;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.exception.YyghException;
import com.example.helper.HttpRequestHelper;
import com.example.model.hosp.Department;
import com.example.model.hosp.Hospital;
import com.example.model.hosp.Schedule;
import com.example.result.Result;
import com.example.result.ResultCodeEnum;
import com.example.service.DepartmentService;
import com.example.service.HospitalService;
import com.example.service.HospitalSetService;
import com.example.service.ScheduleService;
import com.example.utils.MD5;
import com.example.vo.hosp.DepartmentQueryVo;
import com.example.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //删除排班
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String) parameterMap.get("hoscode");
        //签名校验
        //获取医院系统传递过来的签名,签名已进行MD5加密
        String hospSign = (String)parameterMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD%加密
        String encryptMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(encryptMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String hosScheduleId = (String) parameterMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    //查询排班
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String) parameterMap.get("hoscode");
        //签名校验
        //获取医院系统传递过来的签名,签名已进行MD5加密
        String hospSign = (String)parameterMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD%加密
        String encryptMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(encryptMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String depcode = (String) parameterMap.get("depcode");
        int page = StringUtils.isEmpty(parameterMap.get("page")) ? 1 : Integer.parseInt((String) parameterMap.get("page"));
        int limit = StringUtils.isEmpty(parameterMap.get("limit")) ? 1 : Integer.parseInt((String) parameterMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //上传排班
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String) parameterMap.get("hoscode");
        //签名校验
        //获取医院系统传递过来的签名,签名已进行MD5加密
        String hospSign = (String)parameterMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD%加密
        String encryptMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(encryptMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(parameterMap);
        return Result.ok();
    }

    //删除科室
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String) parameterMap.get("hoscode");
        //签名校验
        //获取医院系统传递过来的签名,签名已进行MD5加密
        String hospSign = (String)parameterMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD%加密
        String encryptMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(encryptMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String depcode = (String) parameterMap.get("depcode");
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //查询科室
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String) parameterMap.get("hoscode");
        //签名校验
        //获取医院系统传递过来的签名,签名已进行MD5加密
        String hospSign = (String)parameterMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD%加密
        String encryptMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(encryptMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        int page = StringUtils.isEmpty(parameterMap.get("page")) ? 1 : Integer.parseInt((String) parameterMap.get("page"));
        int limit = StringUtils.isEmpty(parameterMap.get("limit")) ? 1 : Integer.parseInt((String) parameterMap.get("limit"));
        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String) parameterMap.get("hoscode");
        //签名校验
        //获取医院系统传递过来的签名,签名已进行MD5加密
        String hospSign = (String)parameterMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD%加密
        String encryptMD5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!hospSign.equals(encryptMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service里的方法
        departmentService.save(parameterMap);
        return Result.ok();
    }

    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);
        //获取传递的医院编号
        String hoscode = (String) parameterMap.get("hoscode");
        //获取医院系统传递过来的签名,签名已进行MD5加密
        String hospSign = (String)parameterMap.get("sign");

        //根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //把数据库查出来的签名进行MD%加密
        String encryptMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!hospSign.equals(encryptMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法实现根据编号进行查询
        Hospital hospital=hospitalService.getByHostcode(hoscode);
        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院系统传递过来的签名,签名已进行MD5加密
        String hospSign = (String)parameterMap.get("sign");

        //根据传递过来的医院编号，查询数据库，查询签名
        String hoscode = (String) parameterMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //把数据库查出来的签名进行MD%加密
        String encryptMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!hospSign.equals(encryptMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //图片传输过程中“+”转换为了“ ”，需要转换回来
        String logoData = (String) parameterMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        parameterMap.put("logoData",logoData);

        //调用service方法
        hospitalService.save(parameterMap);
        return Result.ok();

    }
}
